using System;
using GoogleMobileAds.Api;
using UnityEngine;

public class AdMobCtrl : MonoBehaviour
{
	//public static AdMobCtrl instance;

	private string adUnitID = "ca-app-pub-4093076135367258~4927037282";

	private string regularID = "ca-app-pub-4093076135367258/6573917603";

	private string rewardID = "ca-app-pub-4093076135367258/4543893904";

	private string[] testingDeviceList = new string[4] { "9358b76be0fd845fe9851f1f202f4db1", "766387a550ff8e7fe2d9045f760d93b7", "1bf52b8bc7ebdf48de4017e0802b9729", "7804c0202f0308a20de915bb3f27402d" };

	public bool ShowRegularAdSwitch;

	public bool ShowRewardAdSwitch;

	public GameObject GO_JustTXT_TestDevice;

	private InterstitialAd regularAD;

	[HideInInspector]
	public RewardBasedVideoAd rewardBasedVideo;

	[HideInInspector]
	public bool rewardVideoClosed;

	private void Awake()
	{
		/* if (instance != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			instance = this;
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		} */
		Init();
	}

	public void Init()
	{
		string deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
		for (int i = 0; i < testingDeviceList.Length; i++)
		{
			if (testingDeviceList[i] == deviceUniqueIdentifier)
			{
				adUnitID = "ca-app-pub-3940256099942544~3347511713";
				regularID = "ca-app-pub-3940256099942544/1033173712";
				rewardID = "ca-app-pub-3940256099942544/5224354917";
				GO_JustTXT_TestDevice.SetActive(true);
				break;
			}
		}
		MobileAds.Initialize(adUnitID);
		if (ShowRegularAdSwitch)
		{
			regularAD = new InterstitialAd(regularID);
			regularAD.OnAdClosed += HandleOnAdClosed;
			RequestRegularAd();
		}
		if (ShowRewardAdSwitch)
		{
			rewardBasedVideo = RewardBasedVideoAd.Instance;
			rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
			rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
			RequestRewardVideo();
		}
	}

	public void RequestRegularAd()
	{
		AdRequest request = new AdRequest.Builder().Build();
		regularAD.LoadAd(request);
		DebugText.instance.Show("RegularAd Loaded");
	}

	public void ShowRegularAD()
	{
		if (ShowRegularAdSwitch && regularAD.IsLoaded())
		{
			regularAD.Show();
		}
	}

	public void HandleOnAdClosed(object sender, EventArgs args)
	{
		RequestRegularAd();
	}

	private void RequestRewardVideo()
	{
		AdRequest request = new AdRequest.Builder().Build();
		rewardBasedVideo.LoadAd(request, rewardID);
	}

	public void ShowRewardVideo()
	{
		rewardVideoClosed = false;
		if (rewardBasedVideo.IsLoaded())
		{
			rewardBasedVideo.Show();
		}
	}

	public void HandleRewardBasedVideoRewarded(object sender, Reward args)
	{
		TimeReward.instance.GetVideoRewardAdGems();
	}

	public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
	{
		RequestRewardVideo();
		rewardVideoClosed = true;
	}
}
